#include "stdafx.h"
#include "wf_zoompan.h"
#include "pf_snapper.h"
#include "pf_point.h"


WF_ZoomPan::WF_ZoomPan(PF_Container& container,
                                   PF_GraphicView& graphicView)
        :PF_ActionInterface("Zoom Pan", container, graphicView) {}


QAction* WF_ZoomPan::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	// tr("Pan Zoom")
	QAction* action = new QAction(tr("&Pan Zoom"), NULL);
	action->setIcon(QIcon(":/actions/zoompan.png"));
	//action->zetStatusTip(tr("Realtime Panning"));
	
    return action;
}


void WF_ZoomPan::init(int status) {
    PF_ActionInterface::init(status);
    snapMode = LX_Define::SnapFree;
    snapRes = LX_Define::RestrictNothing;
    //v1 = v2 = PF_Vector(false);
    x1 = y1 = x2 = y2 = -1;
    //graphicView->saveView();
}



void WF_ZoomPan::trigger() {
    /*if (v1.valid && v2.valid) {
        graphicView->zoomPan(v2-v1);
        v1 = v2;
}*/
        graphicView->zoomPan(x2-x1, y2-y1);
        x1 = x2;
        y1 = y2;
}



void WF_ZoomPan::mouseMoveEvent(PF_MouseEvent* e) {
    //v2 = snapPoint(e);
    x2 = e->x();
    y2 = e->y();
    //if (getStatus()==1 && graphicView->toGuiDX((v2-v1).magnitude())>10) {
    if (getStatus()==1 && (abs(x2-x1)>7 || abs(y2-y1)>7)) {
        trigger();
    }
}



void WF_ZoomPan::mousePressEvent(PF_MouseEvent* e) {
#if QT_VERSION < 0x040700
    if (e->button()==Qt::MidButton ||
#else
    if (e->button()==Qt::MiddleButton ||
#endif
            e->button()==Qt::LeftButton) {
        //v1 = snapPoint(e);
        x1 = e->x();
        y1 = e->y();
        setStatus(1);
    }
}



void WF_ZoomPan::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::RightButton) {
        init(getStatus()-1);
#if QT_VERSION < 0x040700
    } else if (e->button()==Qt::MidButton) {
#else
    } else if (e->button()==Qt::MiddleButton) {
#endif
        init(-1);
    } else {
        setStatus(0);
    }

    //PF_DEBUG->print("PF_ActionZoomPan::mousePressEvent(): %f %f", v1.x, v1.y);
}



void WF_ZoomPan::updateMouseCursor() {
#ifndef __APPLE__
    graphicView->setMouseCursor(LX_Define::SizeAllCursor);
#endif
}


// EOF
